import pygame, sys, math
pygame.init()

clock = pygame.time.Clock()

def distance(pt1, pt2):
    x1 = pt1[0]
    y1 = pt1[1]
    x2 = pt2[0]
    y2 = pt2[1]
    
    return math.sqrt(((x2-x1)**2)+((y2-y1)**2))

width = 640
height = 480
size = width, height

screen = pygame.display.set_mode(size)

bgColor = r,g,b = 0,0,0

ball1Image = pygame.image.load("ball.png")
ball1Rect = ball1Image.get_rect()
speed1x = 2
speed1y = 3
ball1Speed = [speed1x, speed1y]
ball1Radius = ball1Rect.width/2

ball2Image = pygame.image.load("ball.png")
ball2Rect = ball2Image.get_rect()
speed2x = 3
speed2y = 3
ball2Speed = [speed2x, speed2y]
ball2Radius = ball2Rect.width/2

ballPImage = pygame.image.load("playerBall.png")
ballPRect = ballPImage.get_rect()
speedPx = 0
speedPy = 0
ballPSpeed = [speedPx, speedPy]
ballPRadius = ballPRect.width/2

ball2Rect = ball2Rect.move(150,150)
ballPRect = ballPRect.move(width/2,height-100)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                ballPSpeed[0] = 3
            if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                ballPSpeed[0] = -3
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                ballPSpeed[0] = 0
            if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                ballPSpeed[0] = 0
          
    ball1Rect = ball1Rect.move(ball1Speed)
    ball2Rect = ball2Rect.move(ball2Speed)
    ballPRect = ballPRect.move(ballPSpeed)
    
    if ball1Rect.left < 0 or ball1Rect.right > width:
        ball1Speed[0] = -ball1Speed[0]
    if ball1Rect.top < 0 or ball1Rect.bottom > height:
        ball1Speed[1] = -ball1Speed[1]
        
    if ball2Rect.left < 0 or ball2Rect.right > width:
        ball2Speed[0] = -ball2Speed[0]
    if ball2Rect.top < 0 or ball2Rect.bottom > height:
        ball2Speed[1] = -ball2Speed[1]
        
    if ballPRect.left < 0 or ballPRect.right > width:
        ballPSpeed[0] = 0
    if ballPRect.top < 0 or ballPRect.bottom > height:
        ballPSpeed[1] = 0
        
    if ball1Rect.right > ball2Rect.left and ball1Rect.left < ball2Rect.right:
        if ball1Rect.bottom > ball2Rect.top and ball1Rect.top < ball2Rect.bottom:
            print ball1Radius + ball2Radius, distance(ball1Rect.center, ball2Rect.center)
            if ball1Radius + ball2Radius > distance(ball1Rect.center, ball2Rect.center):
                ball1Speed[0] = -ball1Speed[0]
                ball1Speed[1] = -ball1Speed[1]
                ball2Speed[0] = -ball2Speed[0]
                ball2Speed[1] = -ball2Speed[1]
            
    if ballPRect.right > ball1Rect.left and ballPRect.left < ball1Rect.right:
        if ballPRect.bottom > ball1Rect.top and ballPRect.top < ball1Rect.bottom:
            if ballPRadius + ball1Radius > distance(ballPRect.center, ball1Rect.center):
                ball1Speed[0] = -ball1Speed[0]
                ball1Speed[1] = -ball1Speed[1]
                
    if ballPRect.right > ball2Rect.left and ballPRect.left < ball2Rect.right:
        if ballPRect.bottom > ball2Rect.top and ballPRect.top < ball2Rect.bottom:
            if ballPRadius + ball2Radius > distance(ballPRect.center, ball2Rect.center):
                ball2Speed[0] = -ball2Speed[0]
                ball2Speed[1] = -ball2Speed[1]
        
    screen.fill(bgColor)
    screen.blit(ball1Image, ball1Rect)
    screen.blit(ball2Image, ball2Rect)
    screen.blit(ballPImage, ballPRect)
    pygame.display.flip()
    clock.tick(60)
    
    
    
    
    
    
print "hi" 
    
    
    
    
            
    
    
    
    
    
    
    
    
    
    
 
 
 
 
 